from django.db import models
from dvadmin.utils.models import CoreModel, table_prefix


class NodeTypes(CoreModel):
    NODE_TYPE = (
        ("testproject", "测试项目"),
        ("testsuite", "测试套件"),
        ("testcase", "测试用例"),
        ("testcase_version", "用例版本"),
        ("testplan", "测试计划"),
        ("requirement_spec", "需求文档"),
        ("requirement", "需求条目"),
        ("requirement_version", "需求版本"),
        ("testcase_step", "测试步骤"),
        ("requirement_revision", "需求变更"),
        ("requirement_spec_revision", "文档修订"),
        ("build", "测试构建"),
        ("platform", "测试平台"),
        ("user", "测试用户"),
        ("testmodel", "测试模型"),
        ("allpairs", "正交试验"),
        ("decision", "决策表法"),
        ("flow ", "流程图法"),
        ("xmind", "思维导图")
    )
    description = models.CharField(max_length=255, choices=NODE_TYPE, default="testmodel", verbose_name="业务类型", help_text="业务类型")

    class Meta:
        db_table = table_prefix + "node_types"
        verbose_name = "业务类型表"
        verbose_name_plural = verbose_name


class NodesHierarchy(CoreModel):
    name = models.CharField(max_length=255, null=True, blank=True, db_index=True, verbose_name="名称", help_text="名称")
    node_order = models.IntegerField(default=0, verbose_name="用户类型", help_text="用户类型")
    parent = models.ForeignKey(to="NodesHierarchy", on_delete=models.PROTECT, db_constraint=False, null=True, blank=True, verbose_name="上级节点", help_text="上级节点")
    node_type = models.ForeignKey(to="NodeTypes", on_delete=models.PROTECT, db_index=True, verbose_name="关联类型", help_text="关联类型")

    @classmethod
    def recursion_hier_info(cls, hier_id: int, hier_all_list=None, hier_list=None):
        """
        递归获取所有下级节点
        :param hier_id: 需要获取的id
        :param hier_all_list: 所有列表
        :param hier_list: 递归list
        :return:
        """
        if not hier_all_list:
            hier_all_list = NodesHierarchy.objects.values("id", "parent")
        if hier_list is None:
            hier_list = [hier_id]
        for ele in hier_all_list:
            if ele.get("parent") == hier_id:
                hier_list.append(ele.get("id"))
                cls.recursion_hier_info(ele.get("id"), hier_all_list, hier_list)
        return list(set(hier_list))

    class Meta:
        db_table = table_prefix + "nodes_hierarchy"
        verbose_name = "业务树"
        verbose_name_plural = verbose_name


class AllPairs(CoreModel):
    name = models.CharField(max_length=255, null=True, blank=True, db_index=True, verbose_name="名称", help_text="名称")
    factor = models.CharField(max_length=500, db_index=True, verbose_name="因子", help_text="因子")
    level = models.CharField(max_length=500, verbose_name="水平", help_text="水平")
    hierarchy = models.ForeignKey(to="NodesHierarchy", on_delete=models.CASCADE, db_constraint=False, verbose_name="业务树", help_text="业务树")

    class Meta:
        db_table = table_prefix + "model_allpairs"
        verbose_name = "正交试验"
        verbose_name_plural = verbose_name


class Decision(CoreModel):
    STUB_TYPE = (
        ("condition", "条件桩"),
        ("action", "动作桩")
    )
    name = models.CharField(max_length=255, null=True, blank=True, db_index=True, verbose_name="名称", help_text="名称")
    stub = models.CharField(max_length=255, choices=STUB_TYPE, default="condition", verbose_name="桩类型", help_text="桩类型")
    factor = models.CharField(max_length=500, db_index=True, verbose_name="因子", help_text="因子")
    level = models.CharField(max_length=500, verbose_name="水平", help_text="水平")
    hierarchy = models.ForeignKey(to="NodesHierarchy", on_delete=models.CASCADE, db_constraint=False, verbose_name="业务树", help_text="业务树")

    class Meta:
        db_table = table_prefix + "model_decision"
        verbose_name = "判定表"
        verbose_name_plural = verbose_name

class XMind(CoreModel):
    name = models.CharField(max_length=255, null=True, blank=True, db_index=True, verbose_name="名称", help_text="名称")
    note = models.TextField(null=True, blank=True, verbose_name="备注", help_text="备注")
    status = models.BooleanField(default=True, verbose_name="状态", help_text="状态")
    file_list_id = models.BigIntegerField(verbose_name="附件列表id", help_text="附件列表id")
    hierarchy = models.ForeignKey(to="NodesHierarchy", on_delete=models.CASCADE, db_constraint=False, verbose_name="业务树", help_text="业务树")

    class Meta:
        db_table = table_prefix + "model_xmind"
        verbose_name = "思维导图"
        verbose_name_plural = verbose_name

class Flow(CoreModel):
    name = models.CharField(max_length=255, null=True, blank=True, db_index=True, verbose_name="名称", help_text="名称")
    status = models.BooleanField(default=True, verbose_name="状态", help_text="状态")
    file_list_id = models.BigIntegerField(verbose_name="附件列表id", help_text="附件列表id")
    hierarchy = models.ForeignKey(to="NodesHierarchy", on_delete=models.CASCADE, db_constraint=False,
                                  verbose_name="业务树", help_text="业务树")
    class Meta:
        db_table = table_prefix + "model_flow"
        verbose_name = "流程图"
        verbose_name_plural = verbose_name
